//
//  ACSystemFunction.m
//  ACFramework
//
//  Created by caoyq on 2018/6/11.
//  Updated by caoyq on 2018/7/31.
//

#import "ACSystemFunction.h"
#import <AudioToolbox/AudioToolbox.h>
#import <AVFoundation/AVFoundation.h>

@implementation ACSystemFunction

#pragma mark - 声音

+ (void)soundOfSystemShock
{
    AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
}

+ (void)soundOfSystemNotification
{
    AudioServicesPlaySystemSound(1007);
}

+ (void)soundOfSystemFromPath: (NSString *)path isShock: (BOOL)isShock
{
    SystemSoundID soundId;
    AudioServicesCreateSystemSoundID((__bridge CFURLRef)([NSURL fileURLWithPath:path]), &soundId);
    if (isShock) {
        if (@available(iOS 9.0, *)) {
            AudioServicesPlayAlertSoundWithCompletion(soundId, nil);
        } else {
            AudioServicesPlaySystemSound(soundId);
        }
    }else {
        AudioServicesPlaySystemSound(soundId);
    }
}

#pragma mark - 手电筒

+ (void)turnTorch:(BOOL)on
{
    AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType: AVMediaTypeVideo];
    if ([device hasTorch]) {
        if (on) {  // 开启手电筒
            if(device.torchMode != AVCaptureTorchModeOn ||
               device.flashMode != AVCaptureFlashModeOn){
                [device lockForConfiguration:nil];
                [device setTorchMode:AVCaptureTorchModeOn];
                [device setFlashMode:AVCaptureFlashModeOn];
                [device unlockForConfiguration];
            }
        } else { // 关闭手电筒
            if(device.torchMode != AVCaptureTorchModeOff ||
               device.flashMode != AVCaptureFlashModeOff){
                [device lockForConfiguration:nil];
                [device setTorchMode:AVCaptureTorchModeOff];
                [device setFlashMode:AVCaptureFlashModeOff];
                [device unlockForConfiguration];
            }
        }
    }
}

#pragma mark - 粘贴板

+ (void)wirteToPasteboardWithText: (NSString *)text
{
    UIPasteboard *pb = [UIPasteboard generalPasteboard];
    pb.string = text;
}

@end
